package com.air.service.impl;

import cn.hutool.core.util.StrUtil;
import com.air.entity.Dept;
import com.air.entity.Employee;
import com.air.mapper.EmployeeMapper;
import com.air.service.IDeptService;
import com.air.service.IEmployeeService;
import com.air.util.PageResult;
import com.air.vo.EmployeeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private IDeptService deptService;

    @Override
    public PageResult<Employee> findByPage(Integer page, Integer pageSize){
        Page<Employee> page1 = this.page(new Page<>(page,pageSize));

        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    @Override
    public PageResult<Employee> search(Integer page, Integer pageSize, EmployeeVo employeeVo) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(employeeVo.getEname()),"ename", employeeVo.getEname());
        wrapper.like(StrUtil.isNotBlank(employeeVo.getAddr()),"addr", employeeVo.getAddr());
        wrapper.eq(employeeVo.getDid() != null && employeeVo.getDid() != 0, "did",employeeVo.getDid());
        Page<Employee> page1 = this.page(new Page<>(page,pageSize),wrapper);
        page1.getRecords().forEach(student -> {
            Dept dp = deptService.getById(student.getDid());
            student.setDname(dp.getDname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
